package com.leetcode.partition4;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/11/3 18:04
 */
public class LC300最长递增子序列 {

    public static int lengthOfLIS(int[] nums) {
        return dynamicProgramming(nums);
    }

    public static void main(String[] args) {
        int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};
        System.out.println(lengthOfLIS(nums));
    }

    private static int dynamicProgramming(int[] nums) {
        //dp[i]：表示nums[0, i]之间的最长递增子序列个数
        int[] dp = new int[nums.length];
        //初始化，每一个以自己开头的必定有最长递增子序列1
        Arrays.fill(dp, 1);
        int answer = 1;
        //区间dp，枚举每一个nums[j,i]区间
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            answer = Math.max(answer, dp[i]);
        }
        System.out.println(Arrays.toString(dp));
        return answer;
    }

    private static int backtracking(final int[] nums, int startIndex, List<Integer> path, Map<List<Integer>, Integer> cache) {
        if (startIndex >= nums.length) return path.size();
        if (cache.containsKey(path)) return cache.get(path);
        int res = 0;
        for (int i = startIndex; i < nums.length; i++) {
            if (i == nums.length - 1) res = Math.max(res, path.size());
            //不满足递增子序列的进行剪枝
            if (path.size() > 0 && nums[i] <= path.get(path.size() - 1)) continue;
            path.add(nums[i]);
            res = Math.max(backtracking(nums, i + 1, path, cache), res);
            path.remove(path.size() - 1);
            cache.put(path, res);
        }
        return res;
    }
}
